﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Settings;

namespace MASSBAckUp
{
    public partial class DeviceSettings : Form
    {
        private MassStorageDevice Device;
        private SyncFolder ActualFolder = null;

        public DeviceSettings(MassStorageDevice Device)
        {
            this.Device = Device;
            InitializeComponent();
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void AddFolderButton_Click(object sender, EventArgs e)
        {
            FolderSelection fs = new FolderSelection(Device.ActualDiskLetter +@":\");
            if (fs.ShowDialog() == DialogResult.OK)
            {
                SyncFolder sf = new SyncFolder();
                sf.Path = fs.SelectedFolder;
                Device.folders.Add(sf);
                Device.folders.Sort();
                DeviceSettingsWasUpdated(sf);
            }
        }

        private void DeviceSettingsWasUpdated(SyncFolder sf)
        {
            DeleteFolderButton.Enabled = false;
            ClearFileSettingsControls();
            FoldersList.Items.Clear();
            foreach(Object ob in Device.folders)
            {
                 SyncFolder folder = (SyncFolder) ob;
                 int indx = FoldersList.Items.Add(folder);
                 if (folder == sf)
                 {
                     FoldersList.SelectedIndex = indx;
                 }

            }
            
        }

        private void DeviceSettings_Load(object sender, EventArgs e)
        {
            this.Text = "Настрйоки для устройства " + Device.Name + " (" + Device.SerialNumber + ")";
            DeviceSettingsWasUpdated(null);


            toolTip.SetToolTip(AddMaskButton, @"Добавить маску");
            toolTip.SetToolTip(DelMaskButton, @"Добавить маску");
        }

        private void DeleteFolderButton_Click(object sender, EventArgs e)
        {
            Device.folders.Remove(FoldersList.Items[FoldersList.SelectedIndex]);
            DeviceSettingsWasUpdated(null);
        }

        private void FoldersList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FoldersList.SelectedIndex >= 0)
            {
                DeleteFolderButton.Enabled = true;
                ActualFolder = (SyncFolder) FoldersList.Items[FoldersList.SelectedIndex];
                UpdateFileSettingsControls();
            }
            else
            {
                ActualFolder = null;
                DeleteFolderButton.Enabled = false;
                ClearFileSettingsControls();
            }
        }

        

        private void SetRadioButton(Control.ControlCollection Controls, String TagValue)
        {
            foreach (Control ctrl in Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                {
                    RadioButton rb = (RadioButton)ctrl;
                    if (ctrl.Tag.ToString() == TagValue)
                    {
                        rb.Checked = true;
                    }
                    else
                    {
                        rb.Checked = false;
                    }
                }
            }
        }

        private void ClearFileSettingsControls()
        {
            FolderSettingsPanel.Enabled = false;
            FileSettingGroupBox.Text = null;
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.GetType() == typeof(RadioButton))
                {
                    RadioButton rb = (RadioButton)ctrl;
                    rb.Checked = false;
                }
            }

            DestinationFolderText.Text = null;
            FolderTemplateText.Text = null;
            ShowTemplateResult(FolderTemplateText.Text);

            MasksList.Items.Clear();
        }

        private void UpdateFileSettingsControls()
        {
            FolderSettingsPanel.Enabled = true;
            FileSettingGroupBox.Text = ActualFolder.Path;

            SyncSubFolders.Checked = ActualFolder.ProcessSubfolders;

            SetRadioButton(SyncTypeGroup.Controls, ActualFolder.SType.ToString());
            HideFolderTemplate(ActualFolder.SType);

            SetRadioButton(CopyType.Controls, ActualFolder.CType.ToString());

            SetRadioButton(CopyFormSettings.Controls, ActualFolder.CopyForm.ToString());

            DestinationFolderText.Text = ActualFolder.DestinationFolder;

            FolderTemplateText.Text = ActualFolder.FolderTemplate;

            ShowTemplateResult(FolderTemplateText.Text);

            MasksList.Items.Clear();
            foreach (Object ob in ActualFolder.Masks)
            {
                String mask = (String)ob;
                MasksList.Items.Add(mask);
            }
        }

        private void SyncSubFolders_CheckStateChanged(object sender, EventArgs e)
        {
            if (ActualFolder != null)
            {
                ActualFolder.ProcessSubfolders = SyncSubFolders.Checked;
            }
        }

        private void DestinationFolderText_TextChanged(object sender, EventArgs e)
        {
            ActualFolder.DestinationFolder = DestinationFolderText.Text;
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void SyncType_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Checked == true)
            {
                ActualFolder.SType = (SyncFolder.SyncType)System.Enum.Parse(typeof(SyncFolder.SyncType), (String)rb.Tag);
                HideFolderTemplate(ActualFolder.SType);
            }
            
        }

        private void HideFolderTemplate(SyncFolder.SyncType SyncType)
        {
            if (SyncType == SyncFolder.SyncType.ToSelectedFolder)
            {
                FolderTemplateLabel.Visible = false;
                FolderTemplateText.Visible = false;
                TemplateResultsLabel.Visible = false;
            }
            else
            {
                FolderTemplateLabel.Visible = true;
                FolderTemplateText.Visible = true;
                TemplateResultsLabel.Visible = true;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DestinationFolderDialog.SelectedPath = ActualFolder.DestinationFolder;
            if (DestinationFolderDialog.ShowDialog() == DialogResult.OK)
            {
                ActualFolder.DestinationFolder = DestinationFolderDialog.SelectedPath;
                UpdateFileSettingsControls();
            }
        }

        private void CopyType_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Checked == true)
            {
                ActualFolder.CType = (SyncFolder.CopyType)System.Enum.Parse(typeof(SyncFolder.CopyType), (String)rb.Tag);
            }
        }

        private void CopyFormShow_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Checked == true)
            {
                ActualFolder.CopyForm = (SyncFolder.CopyFormShowType)System.Enum.Parse(typeof(SyncFolder.CopyFormShowType), (String)rb.Tag);
            }
        }

        private void FolderTemplateText_TextChanged(object sender, EventArgs e)
        {
            ActualFolder.FolderTemplate = FolderTemplateText.Text;
            ShowTemplateResult(FolderTemplateText.Text);
        }

        private void ShowTemplateResult(String Template)
        {
            if (Template != null && Template.Length > 0)
            {
                try
                {
                    TemplateResultsLabel.Text = DateTime.Now.ToString(Template);
                }
                catch (Exception /*e*/)
                {
                    TemplateResultsLabel.Text = Template;
                }
            }
            else
            {
                TemplateResultsLabel.Text = null;
            }
        }

        private void AddMaskText_TextChanged(object sender, EventArgs e)
        {
            if (AddMaskText.Text != null && AddMaskText.Text.Length == 0)
            {
                AddMaskButton.Enabled = false;
            }
            else
            {
                int i = ActualFolder.Masks.BinarySearch(AddMaskText.Text);
                if (i >= 0)
                {
                    AddMaskButton.Enabled = false;
                }
                else
                {
                    AddMaskButton.Enabled = true;
                }
            }
        }

        private void AddMaskButton_Click(object sender, EventArgs e)
        {
            ActualFolder.Masks.Add(AddMaskText.Text);
            ActualFolder.Masks.Sort();
            AddMaskText.Text = null;
            UpdateFileSettingsControls();
        }

        private void toolTip1_Popup(object sender, PopupEventArgs e)
        {
           
        }

        private void MasksList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int indx = MasksList.SelectedIndex;
            if (indx >= 0)
            {
                DelMaskButton.Enabled = true;
            }
            else
            {
                DelMaskButton.Enabled = false;
            }
        }

        private void DelMaskButton_Click(object sender, EventArgs e)
        {
            ActualFolder.Masks.Remove(MasksList.Items[MasksList.SelectedIndex]);
            DeviceSettingsWasUpdated(ActualFolder);
        }

    }
}
